<template>
  <div class="">
    <comlun-list
      :colunms="headGroup.get(2)"
      :datas="listData"
      :title="mydata.title || ''"
      footTitle="收起"
      isCollapsed
    />
    <template v-if="listData.subList && listData.subList.length">
      <template v-for="(item, index) in listData.subList">
        <comlun-list
          :allColunms="weidiaoArr"
          :originalData="originalData"
          :key="index"
          :colunms="headGroup.get(1)"
          :datas="item"
          :title="`调整数据` + (index + 1)"
          :footTitle="mydata.title.includes('追加') ? '展开未调整字段':'展开未调整字段'"
        />
      </template>
    </template>
  </div>
</template>

<script type="text/ecmascript-6">
/*eslint-disable*/
import { dynamicQueryHeader, budgetFindByFormGuid } from '@/api/detail/index'
import ComlunList from './child/ColumList/index.vue'
import loadingType from '@/views/DetailPage/mixinsloadingtype'

export default {
  data() {
    return {
      headGroup:new Map(),
      listData:{},
      weidiaoArr:[],
      originalData:{},   //原始数据
      needsChangeValue: { //需要修改value的数据
        'isBuy':'采购',
        'budgetType':'调整数据'
      },
      //第一层需要展示的数据
      OTHER_COLUNMS: [ 
          {
            title:'联系人',
            dataIndex:'contacts'
          },
          {
            title:'手机号:',
            dataIndex:'tel'
          },
          {
            title:'调整依据文号',
            dataIndex:'fileNo'
          },
          {
            title:'情况说明',
            dataIndex:'reason'
          },
          {
            title:'受理单号',
            dataIndex:'formGuid'
          },
          {
            title:'创建时间',
            dataIndex:'createTime'
          },
          {
            title:'状态',
            dataIndex:'budgetType'
          }
        ]
    }
  },
  mixins:[loadingType],
  methods: {
    //todo 获取表体
    async budgetFindByFormGuid(data){
      return await budgetFindByFormGuid(data)
    },
    //todo 获取表头
    async dynamicQueryHeader(data){
      return await dynamicQueryHeader(data)
    },
    //验证是否出错
    isErrorRequest(data){
      if (data.code === 200 && data.success) {
        if (data.data) {
          return data.data
        }
        return ''
      }else{
        // this.$message.warning(data.message)
        return ''
      }
    },
    //表头分组
    tableHeadGroup(data){
      if (data && data.length) {
        const headMap = new Map()
        data.forEach(item=>{
          if (headMap.has(item.isChange)) {
            headMap.set(item.isChange,[...headMap.get(item.isChange),item])
          }else{
            headMap.set(item.isChange,[item])
          }
        })
        const { OTHER_COLUNMS } = this
        headMap.set(0,[...OTHER_COLUNMS,...headMap.get(0)])
        headMap.set(2, OTHER_COLUNMS)
        const arr = ['buyCatalogName', 'isBuy', 'budgetType']
        const filterHeadZero = headMap.get(0).filter(item => arr.indexOf(item.dataIndex) === -1)
        headMap.set(0, filterHeadZero)
        return headMap
      }
      return
    },
    async pageInit(){
      const { tableId, title, formGuid } = this.mydata
      const headList = await this.dynamicQueryHeader({
        tableId,
        isVerify: false  //目前不知道取哪儿，写死
      })
      const dealHeadList = this.isErrorRequest(headList)
      const headGroup = this.tableHeadGroup(dealHeadList.headList)
      headGroup ? this.headGroup = headGroup : ''
      if (this.headGroup.size) {
        const yitiao = this.headGroup.get(1)
        if (yitiao && yitiao.length) {
          const arr = []
          yitiao.map(item => {
            arr.push(item.dataIndex)
          })
          //未调整字段
          const weidiao = dealHeadList.headList.filter(item => {
            return arr.indexOf(item.dataIndex) === -1
          })
          if (weidiao && weidiao.length) {
            this.weidiaoArr = weidiao
          }
        }
      }
      const timeArr = title.match(/\d{4}-(\d{2}|\d{1})-(\d{2}|\d{1})/g);
      let year
      if(timeArr && timeArr.length){
        // year = timeArr[0].split('-')[0]   //year
        year = new Date().getFullYear();
      }
      const request = await this.budgetFindByFormGuid({
        year,
        formGuid
      })
      const listData = this.isErrorRequest(request)
      if (listData) {
        const { needsChangeValue } = this
        listData.subList.forEach(item => {
          for(let key in needsChangeValue){
            const value = item[key]
            if (value === 0 || value) {
              let str
              value ? 
                    str = needsChangeValue[key] 
                    :
                    str = `非${needsChangeValue[key]}`;
              
              item[key] = str;
            }
          }
        })
        const supplement = {
            formGuid:this.mydata.formGuid,
            ...listData.subList[0]
          }
        this.listData = { ...listData,...supplement}
        //原始数据
        if (this.mydata.title.includes('指标核减') || this.mydata.title.includes('调整')) {
          this.originalData = this.listData.subList.shift()
        }
        this.dataLoadingType = false
        this.headLoadingType = false
      }


    }
  },
  props:{
    mydata:{
      type:Object,
      default(){
        return {}
      }
    }
  },
  mounted() {
    const {pageInit} = this
    pageInit()
  },
  components:{
    ComlunList
  }
}
</script>

<style lang="less" scoped></style>
